--[[
Name: unitDoodles
Author: Sonora (The Dragonflight, US Kirin Tor)
Website & SVN: http://code.google.com/p/browniehelper/source/browse/#svn/trunk/unitDoodles
License: GNU General Public License v3
]]

--[[
The Tao of Doodles

This file contains the basic configuration for this mod as well as the 
structures which associate doodles with UIObjects.

Two types of doodles are currently defined in doodles.lua: timer bars and 
indicator dots.

Doodles are built based on the config.doodles table.  The primary keys on 
this table are unit names which are mapped to UIFrames according to the 
config.frameSets table and the config.currentFrameSet variable.

A note about point/position parameters.  These should all be 4-member tables:

{pointOnThis, pointOnParent, x-offset, y-offset}

Where pointOnParent and pointOnThis are:

    "TOP", "RIGHT" "BOTTOM" "LEFT": the center-points of the respective sides.
    "TOPRIGHT", "TOPLEFT", "BOTTOMLEFT", "BOTTOMRIGHT": corners of the frame rectangle.
    "CENTER": the center point of the frame rectangle.

These arguments can also be lists of constraints.

Positions can be defined by (in this priority order):

    .setPoint -- absolute position relative to parent unit Frame
    .advancePosition -- position table added to the previous doodle's .setPoint
    .stackPosition -- position table added to the previous VISIBLE doodle's .setPoint
    
More documentation appears below.
]]--

--Runs in application namespace
setfenv(1, unitDoodles)

--Everything in this table should be serializable and configurable with in-game UI
config = {

    --[[
    currentFrameSet defines the key on .frameSets which is currently being used
    to define the names of UIFrame objects in _G which are the parents for
    different units.  This allows us to configure the mod to work with different
    unit frame mods as well as stock UI.
    ]]--
    currentFrameSet = 'oUF',
    
    --[[
    frameSet tables map unit names (unitID values) to UIFrames which will serve
    as parent frames for doodles.  These will be unit frame mod specific.
    
    In addition to unitID keys, each frameSet table should also define
    .playerInGroupFrames = true|false.  This is an unfortunately arcane sounding 
    variable, that does something useful:  some unit frame addons show the 
    player in the party frames, others (like the default) do not.  If yours 
    does, set = true, otherwise set = false.
    
    For party and raid UIFrame name strings use "$p" to denote the party index 
    number (1-5), "$g" to denote raid group number (1-8), and $r to denote the
    raid index number (1-40).
    
    Optionally, frame sets can also define a getFrame function:
        
    .getFrame  f(frameSet, unitID)
    
                Called for any unitID not defined on the frameSet table.  Return
                the Frame object itself.  This allows us to handle frame sets 
                that do things like sort group/raid frames.
                                
    
    This structure is a table associating {frameSetName = frameSetTable, }.
    ]]--
    frameSets = {
    
        default = { 
            player = 'PlayerFrame',
            target = 'TargetFrame',
            targettarget = 'TargetFrameToT',
            pet = 'PetFrame',
            focus = 'FocusFrame',
            --party = 'PartyMemberFrame$p',
            --raid = 'RaidMemberFrame$r', 
            --playerInGroupFrames = false,
        
            getFrame = function(self, unitID)
                if string.sub(unitID, 1, 5) == 'party' then
        
                    local partyFrameName
                    if CompactPartyFrame:IsShown() == 1 then 
                        partyFrameName = 'CompactPartyFrameMember'
                        self.playerInGroupFrames = true
                    else 
                        partyFrameName = 'PartyMemberFrame'
                        self.playerInGroupFrames = false
                    end
            
                    for i = 1,5 do
                        local partyFrame = _G[partyFrameName..i]
                        if partyFrame and partyFrame.unit == unitID then
                            return partyFrame
                        end
                    end
        
                elseif string.sub(unitID, 1, 4) == 'raid' then
        
                    if CompactRaidFrameManager_GetSetting("KeepGroupsTogether") then
                        for groupIdx = 1,8 do
                            for partyIdx = 1,5 do
                                local raidFrame = _G['CompactRaidGroup'..groupIdx..'Member'..partyIdx]
                                if raidFrame and raidFrame.unit == unitID then
                                    return raidFrame
                                end
                            end
                        end
                    else
                        for raidIdx = 1,40 do
                            local raidFrame = _G['CompactRaidFrame'..raidIdx]
                            if raidFrame and raidFrame.unit==unitID then
                                return raidFrame
                            end
                        end
                    end
                end
            end,
        
        },
    
        oUF = {
            player = 'oUF_player',
            target = 'oUF_target',
            targettarget = 'oUF_tot',
            pet = 'oUF_pet',
            focus = 'oUF_focus',
            focustarget = 'oUF_ft',
            party = 'oUF_PartyUnitButton$p',
            raid = 'oUF_Raid$gUnitButton$p',
            playerInGroupFrames = true,
        },
    
        PitBull4 = {
            player = 'PitBull4_Frames_player',
            target = 'PitBull4_Frames_target',
            targettarget = 'PitBull4_Frames_targettarget',
            pet = 'PitBull4_Frames_pet',
            focus = 'PitBull4_Frames_focus',
            focustarget = 'PitBull4_Frames_focustarget',
            party = 'PitBull4_Groups_PartyUnitButton$p',
            raid = 'PitBull_Groups_RaidUnitButton$r',
            playerInGroupFrames = true,
        },
    
        XPerl = {
            player = 'XPerl_Player',
            target = 'XPerl_Target',
            targettarget = 'XPerl_TargetTarget',
            pet = 'XPerl_Player_Pet',
            focus = 'XPerl_Focus',
            focustarget = 'XPerl_FocusTarget',
            party = 'XPerl_party1',
            raid = 'XPerl_Raid_Grp$gUnitButton$p',
            playerInGroupFrames = true,
        },
    
        Grid = { 
            player = 'PlayerFrame',
            target = 'TargetFrame',
            targettarget = 'TargetFrameToT',
            pet = 'PetFrame',
            focus = 'FocusFrame',
            --raid = 'GridLayoutHeader$gUnitButton$p',
        
            getFrame = function(self, unitID)
                local searchUnit
                if unitID == 'party1' then 
                    searchUnit = 'player'
                elseif string.sub(unitID, 1,5) == 'party' then
                    searchUnit = 'party'..string.sub(unitID, 6)-1
                else
                    searchUnit = unitID
                end
                for headerIdx = 1,8 do
                    for unitIdx = 1,5 do
                        local gridFrame = _G['GridLayoutHeader'..headerIdx..'UnitButton'..unitIdx]
                        if gridFrame and gridFrame.unit == searchUnit then
                            return gridFrame
                        end
                    end
                end
            end,
        
            playerInGroupFrames = true,
        },
    
    },
    
    
    --[[
    The unitMacros table defines new "unitId" strings usable in config.doodles 
    that will apply doodles to multiple target unit frames.
    
    unitMacro specified doodles are added to the ends of existing primary unit
    doodle set definitions.
    ]]--
    unitMacros = {    
        playertarget = {'player','target'},
        smallframes = {'targettarget','focus','focustarget','pet'},
        targets = {'target','targettarget','focus','focustarget'},
        group = {'party','raid'}
    
    },
    
    --[[
    doodleDefaults allows us to separate doodle presentation configuration from
    the functionality defined in doodles.lua.  When a configuration is loaded
    the framework will apply attributes defined on tables here to any matching
    doodle class on .doodles.  These settings are inherited.
    ]]--
    doodleDefaults = {
    
        ['doodles.abstract.Dot'] = {
            texture = [=[Interface\Addons\unitDoodles\media\bubbleTex]=],
            presentColor = {0.33, 0.59, 0.33, 1.0}, 
            missingColor = {0.15, 0.15, 0.15, 0.5},
            blendMode = 'BLEND',
            frameLevel = 10,
            height = 10,
            width = 10,
            advancePosition = {'TOPLEFT', 'TOPRIGHT', 1, 0},
        },
    
        ['doodles.abstract.Bar'] = {
            texture = [=[Interface\Addons\unitDoodles\media\barTex]=],
            barColor = {0.33, 0.59, 0.33, 1.0},
            direction = HORIZONTAL,
            backdrop = {
                bgFile = [=[Interface\Tooltips\UI-Tooltip-Background]=],
                insets = {top = -1, left = -1, bottom = -1, right = -1}
            },
            height = 5,
            width = 5,
            stackPosition = {{'TOPLEFT','BOTTOMLEFT',0,-2},{'TOPRIGHT','BOTTOMRIGHT',0,-2}},
        },
    
        ['doodles.aura.Dot'] = {
            aura = '',
            onlyShowMine = false,
        },
    
        ['doodles.aura.Bar'] = {
            aura = '',
            onlyShowMine = true,
        },
    
        ['doodles.aura.debuffHighlight'] = {
            debuffHighlightColors = {
                Disease = {0.6,0.4,0,1},
                Poison = {0,0.6,0,1},
                Curse = {0.6,0,1,1},
                Magic = {0.2,0.6,1,1}
            },
        },
    
        ['doodles.aura.BarColoredByStacks'] = {
            texture = [=[Interface\Addons\unitDoodles\media\barTex]=],
            barColor = {0.10, 0.50, 0.10, 1.0},
            fullStackBarColor = {0.33, 0.59, 0.33, 1.0},
            possibleStacks = 1,
            direction = HORIZONTAL,
            backdrop = {
                bgFile = [=[Interface\Tooltips\UI-Tooltip-Background]=],
                insets = {top = -1, left = -1, bottom = -1, right = -1}
            },
            height = 5,
            stackPosition = {{'TOPLEFT','BOTTOMLEFT',0,-2},{'TOPRIGHT','BOTTOMRIGHT',0,-2}},
        },
    
    },
    
    --[[
    doodleSets are tables which describe lists of doodles to attach to specific
    unit frames (keys are blizzard unitID's or unitMacros).  This is where 
    doodles actually get defined!
    
    doodleType is sugar for inherits = eval(doodleType).
    ]]--
    doodleSets = {
    
        --Class tailored doodle sets
        WARLOCK = {
            targets = {
                {aura = 'Curse of the Elements',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.58, 0.51, 0.79, 0.8},
                    setPoint = {{'TOPLEFT', 'BOTTOMLEFT', 0, -20},{'TOPRIGHT', 'BOTTOMRIGHT', 0, -20}}},
                {aura = 'Bane of Agony',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.95, 0.85, 0.12, 1.0}},
                {aura = 'Bane of Doom',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.95, 0.85, 0.12, 0.7}},
                {aura = 'Corruption',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.41, 0.8, 0.94, 0.8}},
                {aura = 'Unstable Affliction',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.95, 0.28, 0.26, 0.8}},
                {aura = 'Immolate',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.95, 0.28, 0.26, 0.8}},
                {aura = 'Haunt',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.33, 0.80, 0.33, 0.8}},
                {aura = 'Fear',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.31, 0.45, 0.63}},
            
                {aura = 'Seed of Corruption',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.33, 0.59, 0.33, 1.00},
                    missingColor = {0.15, 0.15, 0.15, 0.0},
                    setPoint = {'CENTER', 'CENTER', 0, 15},
                    height = 12.5,
                    width = 12.5},
            },
        
            group = {            
                {aura = 'Demonic Pact',
                    doodleType = 'doodles.aura.Dot',
                    setPoint = {'TOPLEFT', 'TOPLEFT', 2, -5},
                    presentColor = {213/255, 126/255, 59/255, 1.0},
                    missingColor = {0.15, 0.15, 0.15, 0.0}},
                {aura = 'Blood Pact',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.77, 0.12, 0.23, 1.0},
                    missingColor = {0.15, 0.15, 0.15, 0.0}},
                {aura = 'Soulstone Resurrection',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.58, 0.51, 0.79, 1.0},
                    setPoint = {'TOPRIGHT', 'TOPRIGHT', -2, -5},
                    missingColor = {0.15, 0.15, 0.15, 0.0}},
            },
        
            player = {
                {aura = 'Molten Core',
                    doodleType = 'doodles.aura.BarColoredByStacks',
                    barColor = {0.95, 0.85, 0.12, 0.2},
                    fullStackBarColor = {0.95, 0.85, 0.12, 1.0},
                    possibleStacks = 3,
                    setPoint = {{'TOPLEFT', 'BOTTOMLEFT', 0, -30},{'TOPRIGHT', 'BOTTOMRIGHT', 0, -30}}},
                {aura = 'Decimation',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.95, 0.28, 0.26, 0.8}},
            
                {aura = 'Fel Armor',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.33, 0.59, 0.33, 1.0},
                    setPoint = {'CENTER', 'CENTER', 0, 15},
                    height = 12.5,
                    width = 12.5},
            },
        
            focus = {
                {aura = 'Banish',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.33, 0.80, 0.33, 0.8},
                    setPoint = {{'TOPLEFT', 'BOTTOMLEFT', 0, -5},{'TOPRIGHT', 'BOTTOMRIGHT', 0, -5}}},
            },
        
        },

        DRUID = {
            playertarget = {
                {aura = 'Faerie Fire',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.96, 0.55, 0.73, 0.8},
                    setPoint = {{'TOPLEFT', 'BOTTOMLEFT', 0, -20},{'TOPRIGHT', 'BOTTOMRIGHT', 0, -20}}},
                {aura = 'Insect Swarm',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.78, 0.61, 0.43, 0.8}},
                {aura = 'Moonfire',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.41, 0.8, 0.94, 0.8}},
                {aura = 'Lifebloom',
                    doodleType = 'doodles.aura.BarColoredByStacks',
                    barColor = {49/255, 84/255, 49/255, 1.0},
                    fullStackBarColor = {0.33, 0.59, 0.33, 1.0},
                    possibleStacks = 3},
                {aura = 'Rejuvenation',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.96, 0.55, 0.73, 0.8}},
                {aura = 'Regrowth',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.31, 0.45, 0.63}},
                
                {aura = 'Innervate',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.31, 0.45, 0.63, 1},
                    setPoint = {'CENTER', 'CENTER', 0, 15},
                    height = 12.5,
                    width = 12.5},
                {aura = 'Thorns',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {128/255, 94/255, 36/255, 1},
                    height = 12.5,
                    width = 12.5},
            
            },
        
            group = {
                {aura = 'Innervate',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.31, 0.45, 0.63, 1},
                    missingColor = {0.15, 0.15, 0.15, 0.0},
                    setPoint = {'TOPLEFT', 'TOPLEFT', 2, -5}},
                {aura = 'Thorns',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {128/255, 94/255, 36/255, 1},
                    missingColor = {0.15, 0.15, 0.15, 0.0}},
            
                {aura = 'Lifebloom',
                    doodleType = 'doodles.aura.Dot',
                    setPoint = {'TOPRIGHT', 'TOPRIGHT', -2, -5},
                    barColor = {49/255, 84/255, 49/255, 1.0},
                    presentColor = {0.33, 0.59, 0.33, 1.0}},
                {aura = 'Rejuvenation',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.96, 0.55, 0.73, 1.0},
                    advancePosition = {'TOPRIGHT', 'TOPLEFT', -1, 0}},
                {aura = 'Regrowth',
                    doodleType = 'doodles.aura.Dot',
                    presentColor = {0.31, 0.45, 1.0},
                    advancePosition = {'TOPRIGHT', 'TOPLEFT', -1, 0}},
            
                {doodleType = 'doodles.aura.debuffHighlight',
                    setPoint = {'TOPLEFT', 'TOPRIGHT', 2, 0}},
            
            },
        
            focus = {
                {aura = 'Faerie Fire',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.96, 0.55, 0.73, 0.8},
                    setPoint = {{'TOPLEFT', 'BOTTOMLEFT', 0, -5},{'TOPRIGHT', 'BOTTOMRIGHT', 0, -5}}},
                {aura = 'Insect Swarm',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.78, 0.61, 0.43, 0.8}},
                {aura = 'Moonfire',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.41, 0.8, 0.94, 0.8}},
                {aura = 'Lifebloom',
                    doodleType = 'doodles.aura.BarColoredByStacks',
                    barColor = {49/255, 84/255, 49/255, 1.0},
                    fullStackBarColor = {0.33, 0.59, 0.33, 1.0},
                    possibleStacks = 3},
                {aura = 'Rejuvenation',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.96, 0.55, 0.73, 0.8}},
                {aura = 'Regrowth',
                    doodleType = 'doodles.aura.Bar',
                    barColor = {0.31, 0.45, 0.63}},
        
            },
        },
    
        DEATHKNIGHT = {},
        WARRIOR = {},
        PRIEST = {},
        ROGUE = {},
        PALADIN = {},
        MAGE = {},
        SHAMAN = {},
        HUNTER = {},
    },
}

--[[
loadDoodleSets is a list of strings which defines the doodle sets that should be
shown.

By default we try to load a doodle set that matches the current player's class
name.
]]--
config.loadDoodleSets = {select(2,UnitClass("player")),}

